package com.cc.app;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.cc.thread.MyAsyncThread;
import com.rd.PageIndicatorView;
import com.rd.animation.AnimationType;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;

import okhttp3.Call;

public class MainActivity extends BaseActivity {

    private Button mButton;
    private Button btnTest;
    private ViewPager viewPager;
    private PageIndicatorView indicatorView;

    private ArrayList<ImageView> imgList;
    private final static int[] imgArrs = {R.drawable.t_1 , R.drawable.t_2 ,
                                            R.drawable.t_3 , R.drawable.t_4 ,
                                            R.drawable.t_5 , R.drawable.t_6 , };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getWidget();
        initPagerView();
    }

    @Override
    Activity getActivity() {
        return this;
    }

    public void getWidget() {
        viewPager = this.findViewById(R.id.viewPager);
        indicatorView = this.findViewById(R.id.pageIndicatorView);

        mButton = this.findViewById(R.id.mButton);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new MyAsyncThread(MainActivity.this).execute();
            }
        });
        btnTest = this.findViewById(R.id.btnTest);
        btnTest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                test();
            }
        });
    }

    public void initPagerView(){
        imgList = new ArrayList<>();
        for (int i = 0; i < imgArrs.length; i++) {
            ImageView imageView = new ImageView(this);
            imageView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT , LinearLayout.LayoutParams.MATCH_PARENT ));
            imageView.setImageResource(imgArrs[i]);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imgList.add(imageView);
        }

        indicatorView.setAnimationType(AnimationType.THIN_WORM);
        indicatorView.setCount(imgList.size());
        indicatorView.setDynamicCount(true);
        indicatorView.setInteractiveAnimation(true);


        viewPager.setAdapter(new MyViewPagerAdapter(imgList));
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                indicatorView.setSelection(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }


    public class MyViewPagerAdapter extends PagerAdapter{
        private ArrayList<ImageView> mListViews;

        public MyViewPagerAdapter(ArrayList<ImageView> mListViews) {
            this.mListViews = mListViews;//构造方法，参数是我们的页卡，这样比较方便。
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object)   {
            container.removeView(mListViews.get(position));//删除页卡
        }


        @Override
        public Object instantiateItem(ViewGroup container, int position) {  //这个方法用来实例化页卡
            container.addView(mListViews.get(position), 0);//添加页卡
            return mListViews.get(position);
        }

        @Override
        public int getCount() {
            return  mListViews.size();//返回页卡的数量
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0==arg1;//官方提示这样写
        }
    }


    public void test(){
        System.out.println("test-------------------------!");
        String url = "http://wuwu.imalaxiansheng.com/web/getcenter.php";
        OkHttpUtils
                .get()
                .url(url)
//                .addParams("username", "hyman")
//                .addParams("password", "123")
                .build()
                .execute(new StringCallback()
                {
                    @Override
                    public void onError(Call call, Exception e, int i) {
                        System.out.println("1-------------------->"+e.getMessage());
                    }

                    @Override
                    public void onResponse(String s, int i) {
                        System.out.println("2-------------------->"+s);
                    }
                });

    }

}
